package com.mediatime;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.TextView;

public class MainActivity extends Activity {

	int count = 0;
	long upM = 0, downM = 0;
	TextView text;
	SoundPool pool;
	HashMap<Integer, Integer> soundmap;
	private int currStreamId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		startService(new Intent(this, listenerservice.class));
		text = (TextView) findViewById(R.id.text);
		initSoundPool();
	}

	private void initSoundPool() {
		// TODO Auto-generated method stub
		pool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
		soundmap = new HashMap<Integer, Integer>();

		soundmap.put(0, pool.load(this, R.raw.h00, 1));
		soundmap.put(1, pool.load(this, R.raw.h01, 1));
		soundmap.put(2, pool.load(this, R.raw.h02, 1));
		soundmap.put(3, pool.load(this, R.raw.h03, 1));
		soundmap.put(4, pool.load(this, R.raw.h04, 1));
		soundmap.put(5, pool.load(this, R.raw.h05, 1));
		soundmap.put(6, pool.load(this, R.raw.h06, 1));
		soundmap.put(7, pool.load(this, R.raw.h07, 1));
		soundmap.put(8, pool.load(this, R.raw.h08, 1));
		soundmap.put(9, pool.load(this, R.raw.h09, 1));
		soundmap.put(10, pool.load(this, R.raw.h10, 1));
		soundmap.put(11, pool.load(this, R.raw.h11, 1));
		soundmap.put(12, pool.load(this, R.raw.h12, 1));
		soundmap.put(13, pool.load(this, R.raw.h13, 1));
		soundmap.put(14, pool.load(this, R.raw.h14, 1));
		soundmap.put(15, pool.load(this, R.raw.h15, 1));
		soundmap.put(16, pool.load(this, R.raw.h16, 1));
		soundmap.put(17, pool.load(this, R.raw.h17, 1));
		soundmap.put(18, pool.load(this, R.raw.h18, 1));
		soundmap.put(19, pool.load(this, R.raw.h19, 1));
		soundmap.put(20, pool.load(this, R.raw.h20, 1));
		soundmap.put(21, pool.load(this, R.raw.h21, 1));
		soundmap.put(22, pool.load(this, R.raw.h22, 1));
		soundmap.put(23, pool.load(this, R.raw.h23, 1));

		soundmap.put(100, pool.load(this, R.raw.m00, 1));
		soundmap.put(101, pool.load(this, R.raw.m01, 1));
		soundmap.put(102, pool.load(this, R.raw.m02, 1));
		soundmap.put(103, pool.load(this, R.raw.m03, 1));
		soundmap.put(104, pool.load(this, R.raw.m04, 1));
		soundmap.put(105, pool.load(this, R.raw.m05, 1));
		soundmap.put(106, pool.load(this, R.raw.m06, 1));
		soundmap.put(107, pool.load(this, R.raw.m07, 1));
		soundmap.put(108, pool.load(this, R.raw.m08, 1));
		soundmap.put(109, pool.load(this, R.raw.m09, 1));
		soundmap.put(110, pool.load(this, R.raw.m10, 1));
		soundmap.put(111, pool.load(this, R.raw.m11, 1));
		soundmap.put(112, pool.load(this, R.raw.m12, 1));
		soundmap.put(113, pool.load(this, R.raw.m13, 1));
		soundmap.put(114, pool.load(this, R.raw.m14, 1));
		soundmap.put(115, pool.load(this, R.raw.m15, 1));
		soundmap.put(116, pool.load(this, R.raw.m16, 1));
		soundmap.put(117, pool.load(this, R.raw.m17, 1));
		soundmap.put(118, pool.load(this, R.raw.m18, 1));
		soundmap.put(119, pool.load(this, R.raw.m19, 1));
		soundmap.put(120, pool.load(this, R.raw.m20, 1));
		soundmap.put(121, pool.load(this, R.raw.m21, 1));
		soundmap.put(122, pool.load(this, R.raw.m22, 1));
		soundmap.put(123, pool.load(this, R.raw.m23, 1));
		soundmap.put(124, pool.load(this, R.raw.m24, 1));
		soundmap.put(125, pool.load(this, R.raw.m25, 1));
		soundmap.put(126, pool.load(this, R.raw.m26, 1));
		soundmap.put(127, pool.load(this, R.raw.m27, 1));
		soundmap.put(128, pool.load(this, R.raw.m28, 1));
		soundmap.put(129, pool.load(this, R.raw.m29, 1));
		soundmap.put(130, pool.load(this, R.raw.m30, 1));
		soundmap.put(131, pool.load(this, R.raw.m31, 1));
		soundmap.put(132, pool.load(this, R.raw.m32, 1));
		soundmap.put(133, pool.load(this, R.raw.m33, 1));
		soundmap.put(134, pool.load(this, R.raw.m34, 1));
		soundmap.put(135, pool.load(this, R.raw.m35, 1));
		soundmap.put(136, pool.load(this, R.raw.m36, 1));
		soundmap.put(137, pool.load(this, R.raw.m37, 1));
		soundmap.put(138, pool.load(this, R.raw.m38, 1));
		soundmap.put(139, pool.load(this, R.raw.m39, 1));
		soundmap.put(140, pool.load(this, R.raw.m40, 1));
		soundmap.put(141, pool.load(this, R.raw.m41, 1));
		soundmap.put(142, pool.load(this, R.raw.m42, 1));
		soundmap.put(143, pool.load(this, R.raw.m43, 1));
		soundmap.put(144, pool.load(this, R.raw.m44, 1));
		soundmap.put(145, pool.load(this, R.raw.m45, 1));
		soundmap.put(146, pool.load(this, R.raw.m46, 1));
		soundmap.put(147, pool.load(this, R.raw.m47, 1));
		soundmap.put(148, pool.load(this, R.raw.m48, 1));
		soundmap.put(149, pool.load(this, R.raw.m49, 1));
		soundmap.put(150, pool.load(this, R.raw.m50, 1));
		soundmap.put(151, pool.load(this, R.raw.m51, 1));
		soundmap.put(152, pool.load(this, R.raw.m52, 1));
		soundmap.put(153, pool.load(this, R.raw.m53, 1));
		soundmap.put(154, pool.load(this, R.raw.m54, 1));
		soundmap.put(155, pool.load(this, R.raw.m55, 1));
		soundmap.put(156, pool.load(this, R.raw.m56, 1));
		soundmap.put(157, pool.load(this, R.raw.m57, 1));
		soundmap.put(158, pool.load(this, R.raw.m58, 1));
		soundmap.put(159, pool.load(this, R.raw.m59, 1));

	}

	public void playSound(int sound, int loop) {
		AudioManager am = (AudioManager) this
				.getSystemService(Context.AUDIO_SERVICE);

		float streamVolumeCurrent = am
				.getStreamVolume(AudioManager.STREAM_MUSIC);

		float streamVolumeMax = am
				.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

		float volume = streamVolumeCurrent / streamVolumeMax;
		currStreamId = pool.play(soundmap.get(sound), volume, volume, 1, loop,
				1.0f);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		for (int i = 0; i < menu.size(); i++) {
			menu.getItem(i).setOnMenuItemClickListener(
					new OnMenuItemClickListener() {

						@Override
						public boolean onMenuItemClick(MenuItem item) {
							// TODO Auto-generated method stub
							if (item.getItemId() == R.id.action_settings)
								MainActivity.this.finish();
							return false;
						}
					});
		}
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_MEDIA_PAUSE:
		case KeyEvent.KEYCODE_BACK:
			count = (System.currentTimeMillis() - upM) < 1000 ? count + 1 : 0;
			if (count == 2) {
				text.setText(new Date().toString());
				final int[] tm = now();
				// boolean lock = false;
				playSound(tm[0], 0);
				new Thread() {
					public void run() {
						try {
							sleep(1200);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						playSound(tm[1] + 100, 0);
					}
				}.start();
			} else
				text.setText("Hello World!");
			break;
		}
		return false;
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_MEDIA_PAUSE:
		case KeyEvent.KEYCODE_BACK:
			upM = System.currentTimeMillis();
			break;
		}
		return false;
	}

	String now;

	public int[] now() {
		now = new SimpleDateFormat("HH:mm").format(new Date());
		String[] hm = now.split(":");
		return new int[] { Integer.parseInt(hm[0]), Integer.parseInt(hm[1]) };
	}
}
